#-*- encoding:utf-8 -*-
from web import form
from configs import db

def required():
	return form.notnull

def limited(min,max=None):
	if max :
		return form.regexp(r".{%d,%d}$" %(min,max),"请输入%d-%d位字符" %(min,max))
	else :
		return form.regexp(r".{%d,}$" %min,"请输入至少%d位字符" %min)

def email():
	return form.regexp(r".*@.*", "email地址不正确")

def unique(model,name,value,id=None):
	if id :
		record = db.select(model,
			vars = dict(name=name,value=value,id=id),
			what = 'id',
			where = '$name = $value and id != $id'
		)
	else :
		record = db.select(model,
			vars = dict(name=name,value=value),
			what = 'id',
			where = '$name = $value'
		)
	return len(record) > 0